type TApiFunc<TResult> = (params: any) => Promise<TResult>;

/**
 * @description 创建单例模式的Promise函数，pending状态时不会重复发起请求
 * @param target Promise函数
 * @returns 单例模式的Promise函数
 */
export function singletonPromise<TResult>(target: TApiFunc<TResult>) {
	let instance: Promise<TResult> | null = null;

	const singleton = (params: any) => {
		if (!instance) {
			instance = target(params).finally(() => (instance = null));
		}

		return instance;
	};

	return singleton;
}
